/**
 * @description order router
 */

const router=require('koa-router')()
const {SuccessModel,ErrorModel}=require('../res-model/index')
const loginCheck=require('../middleware/loginCheck')
const {setOrder,getOrder}=require('../controller/order')

router.prefix('/api/order')

//创建订单

router.post('/',loginCheck,async function(ctx,next){
    const username=ctx.session.userInfo.username
    const data=ctx.request.body
    try {
        const order=await setOrder(username,data)
        ctx.body=new SuccessModel(order)
    } catch(ex){
        console.error(ex)
        ctx.body=new ErrorModel(10005,'创建订单失败')
    }

})

//获取订单列表

router.get('/',loginCheck,async function(ctx,next){
    const username=ctx.session.userInfo.username
    const order=await getOrder(username)
    ctx.body=new SuccessModel(order)
})

module.exports=router